package com.oj.core.util;

import java.io.*;

/**
 * Created by Bing on 2018/5/3.
 */

/*
        假设测试用例
        #include <stdio.h>
        int main(){

        int a,b;

        scanf("%d %d",&a,&b);
        if(a+b==3){
        printf("%d",a+b);
        }

        return 0;
        }
*/

public class OJCoreJava {

    public void compile(String command) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command);
        //取得命令结果的输出流
        InputStream fis=process.getInputStream();
        //用一个读输出流类去读
        InputStreamReader isr=new InputStreamReader(fis);
        //用缓冲器读行
        BufferedReader br=new BufferedReader(isr);
        String line=null;
        //直到读完为止
        while((line=br.readLine())!=null)
        {
            System.out.println(line);
        }
        br.close();
        isr.close();
        fis.close();
    }
    public void run(String command) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command);

        new Thread(new Runnable() {
            @Override
            public void run() {
                OutputStream os = null ;
                try {
                    Thread.sleep(2*1000);
                    os = process.getOutputStream();
                    BufferedReader proIn = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\idaworkspace\\JavaTest\\check\\input\\001.txt"))));
                    String in = null;
                    while((in=proIn.readLine())!=null){
                        System.out.println("输入："+in);
                        os.write((in+"\n").getBytes());//  \n  很重要
                        os.flush();
                    }
                    System.out.println("input end .....");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        //程序输出
        BufferedReader proOut = new BufferedReader(new InputStreamReader(process.getInputStream()));
        //获取真正的结果
        BufferedReader result = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\idaworkspace\\JavaTest\\check\\output\\001.txt"))));
        String outc = null;
        String resultc = null;
        boolean resultFlag = false;
        while ((outc = proOut.readLine())!=null){
            System.out.println("结果："+outc);
            resultc = result.readLine();
            if(resultc.equals(outc)){//自己加断点看结果  输出已经重置了 没办法输出到控制台
                resultFlag = true;
            }else{
                resultFlag = false;
                break;
            }
        }
        if(resultFlag){
            System.out.println("Accept!");
        }else{
            System.out.println("Error!");
        }
    }

    public static void main(String[] args){

        String code = "package com.bing;\n" +
                "\n" +
                "import java.util.Scanner;\n" +
                "\n" +
                "/**\n" +
                " * Created by Bing on 2018/5/6.\n" +
                " */\n" +
                "public class   Test   {\n" +
                "    public static void main(String[] args){\n" +
                "\n" +
                "        int a,b;\n" +
                "        Scanner scanner = new Scanner(System.in);\n" +
                "        for(int i=0;i<2;i++){\n" +
                "            a = scanner.nextInt();\n" +
                "            b = scanner.nextInt();\n" +
                "            System.out.println(a+b);\n" +
                "        }\n" +
                "    }\n" +
                "}\n";



        //去掉package
        if(code.indexOf("package")>=0){
            code = code.substring(code.indexOf(";")+1);
        }
        //获取类名
        String temp = code.substring(code.indexOf(" class ")+7).trim();
        String className = temp.substring(0,temp.indexOf(" "));
        //替换类名
        String fileName = "XXXXMain";
        code = code.replaceAll(className,fileName);
        System.out.println(code);

        FileOutputStream outputStream = null;
        String filePath = fileName + ".java";
        try {
            outputStream = new FileOutputStream(filePath);
            outputStream.write(code.getBytes());

            System.out.println(System.getProperties().get("user.dir"));
            System.out.println("javac "+fileName+".java");
            //编译
            new OJCoreJava().compile("cmd /c javac "+filePath);
            //运行
            new OJCoreJava().run("cmd /c  java "+fileName);

//            new OJCore().execCmd("cmd /c  D:/test.bat "+filePath+" "+dir.getAbsolutePath()+" "+fileName,dir,fileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
